package com.ruoyi.manage.strategy;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.manage.DTO.ChargingDTO;
import com.ruoyi.manage.strategy.Impl.BasicBillingPolicy;
import com.ruoyi.manage.strategy.Impl.LongDistanceBillingPolicy;
import com.ruoyi.manage.strategy.Impl.OverMileageBillingPolicy;
import com.ruoyi.manage.strategy.Impl.TimeOutBillingPolicy;
import org.springframework.stereotype.Component;

/**
 * @Title:
 * @BelongProjecet online-car-hailing
 * @BelongPackage com.ruoyi.manage.strategy
 * @Description:
 * @Copyright
 * @Author: 马朝斌
 * @Date: 2024-07-03 22:00
 * @Version V1.0
 */
@Component
public class DetermineTheBasicBillingPolicy {

    /**
     * 判断基础计费策略
     * DetermineTheBasicBillingPolicy
     *
     * @param chargingDTO
     * @return
     */

    public AjaxResult determineTheAccountingPolicy(ChargingDTO chargingDTO) {

        /**
         * 判断是否是基础费用
         * 判断订单距离是否大于起步时间 并且小于起步距离
         * 判断订单时间是否小于起步时间 并且大于起步距离
         * 判断订单时间是否大于起步时间 并且大于起步距离
         * 判断是否是长途
         */

        if (chargingDTO == null) {
            return AjaxResult.error("参数错误");
        }

        // 判断是否是基础费用
        if (chargingDTO.getOrderTime() < chargingDTO.getStartingTime()
                && chargingDTO.getOrderDistance() < chargingDTO.getStartingKm()) {
            BillingPolicies billingPolicies = new BasicBillingPolicy();
            return billingPolicies.billingPolicies(chargingDTO);
        } else if ((chargingDTO.getOrderTime() >= chargingDTO.getStartingTime()
                && chargingDTO.getOrderDistance() < chargingDTO.getStartingKm())) {
            // 判断订单距离是否大于起步距离 并且小于起步时间
            BillingPolicies billingPolicies = new TimeOutBillingPolicy();
            return billingPolicies.billingPolicies(chargingDTO);
        } else if ((chargingDTO.getOrderTime() < chargingDTO.getStartingTime()
                && chargingDTO.getOrderDistance() >= chargingDTO.getStartingKm())) {
            // 判断订单时间是否小于起步时间 并且大于起步距离
            BillingPolicies billingPolicies = new OverMileageBillingPolicy();
            return billingPolicies.billingPolicies(chargingDTO);
        } else if ((chargingDTO.getOrderTime() >= chargingDTO.getStartingTime()
                && chargingDTO.getOrderDistance() >= chargingDTO.getStartingKm())) {
            // 判断订单时间是否大于起步时间 并且大于起步距离 获取价格
            // 调用超里程的策略
            BillingPolicies overMileageBillingPolicy = new OverMileageBillingPolicy();
            // 调用超时的策略
            BillingPolicies timeOutBillingPolicy = new TimeOutBillingPolicy();
            // 获取超里程的信息
            AjaxResult overMileageAjaxResult = overMileageBillingPolicy.billingPolicies(chargingDTO);
            ChargingDTO overMileageChargingDTO = (ChargingDTO) overMileageAjaxResult.get("data");
            // 获取超时的信息
            AjaxResult timeOutAjaxResult = timeOutBillingPolicy.billingPolicies(chargingDTO);
            ChargingDTO timeOutChargingDTO = (ChargingDTO) timeOutAjaxResult.get("data");
            // 计算超时、超里程的价格 订单价格 = 超时总费用 + 起步价格 + 超里程总费用
            chargingDTO.setOrderPrice(
                    chargingDTO.getStartingPrice()
                            .add(overMileageChargingDTO.getOutOfTimeTotalPrice())
                            .add(timeOutChargingDTO.getOutOfDistanceTotalPrice()
                            ));
        } else if (chargingDTO.getOrderDistance() > chargingDTO.getLongDistance()) {
            // 判断订单距离是否大于长途距离
            BillingPolicies billingPolicies = new LongDistanceBillingPolicy();
            return billingPolicies.billingPolicies(chargingDTO);
        }

        return AjaxResult.success(chargingDTO);
    }

}
